﻿-- --------------------------------------------------------------------------------- 
-- Script Name:  Post-Deployment Script Template	
--
-- Description: This file contains SQL statements that will be appended to the build script.
--
--
--
-- Version  Date          Author           Description
-- ---------------------------------------------------------------------------------
--  1       27-May-2012   The Collective   Created script
-- 
--
--
--
-- ---------------------------------------------------------------------------------
-- ---------------------------------------------------------------------------------


-- -----------------------------------------------
--  Accounts_Roles Table Values
-- -----------------------------------------------

-- Insert if records do not exist
MERGE INTO dbo.[Accounts_Roles]
   USING (VALUES (101, 'Administrator'),
	             (102, 'Edit All'),
			     (103, 'Read All'),
			     (104, 'Normal User') ) AS source ( RoleNumber, Description )
   ON source.RoleNumber = Accounts_Roles.RoleNumber
WHEN NOT MATCHED THEN 
   INSERT (RoleNumber, Description) 
   VALUES (source.RoleNumber, source.Description);



Go


-- -----------------------------------------------
--  Accounts_User Initial Value
-- -----------------------------------------------

/*
 The initial login is set to;
 username: tcnadmin
 password: changeme
 
 The password is a SHA 512 hash.
 When you attempt to login for the first time you will need to change this password.
 
*/

BEGIN  
   Declare @recordcount int;
   Declare @guid uniqueidentifier;

   Set @guid = newid();
   Set @recordcount = 0;

   begin try
               		       
	   if not exists(Select username from Accounts_user where username = 'tcnadmin')
	   begin
	      -- Begin the transaction
          Begin Transaction create_user_transaction;

	      -- Create the user
		  Insert into Accounts_User (User_GUID, username, passwordencrypt)
		  values (@guid, 'tcnadmin', 'F1891CEA80FC05E433C943254C6BDABC159577A02A7395DFEBBFBC4F7661D4AF56F2D372131A45936DE40160007368A56EF216A30CB202C66D3145FD24380906');
			   		        
		  Insert into Accounts_User_Roles (User_GUID, RoleNumber)
		  values (@guid, '101');

				         
          Commit Transaction create_user_transaction; 
	   
	   end;  		   
                 
   end try
   begin catch
       -- If an error occurs then rollback
       Rollback Transaction create_user_transaction;              
			    
   end catch

END;

Go

